package com.sk.weichat.ui.me.wallet.viewmode;

import android.util.Log;
import com.sk.weichat.bean.AliPayAuthMsgBean;
import com.sk.weichat.ui.me.wallet.view.AliPayView;
import com.sk.weichat.ui.base.CoreManager;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ObjectResult;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class AliPayViewMode {

    private AliPayView mAliPayView;
    private CoreManager coreManager;

    public AliPayViewMode( CoreManager coreManager,AliPayView mAliPayView) {
        this.mAliPayView = mAliPayView;
        this.coreManager = coreManager;
    }
    /**
     * 获取支付宝授权信息绑定支付宝
     */
    public void getAlipayAutoMsg(){
        mAliPayView.showProgress();
        Map<String, String> params = new HashMap<>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        HttpUtils.post().url(coreManager.getConfig().ALIPAY_AUTH_MSG)
                .params(params)
                .build()
                .execute(new BaseCallback<String>(String.class) {

                    @Override
                    public void onResponse(ObjectResult<String> result) {
                        mAliPayView.AlipayAuthMsgResult(result);
                        mAliPayView.hideProgress();
                    }
                    @Override
                    public void onError(Call call, Exception e) {
                        Log.e("AlipayAutoMsg", "Exception", e);
                        mAliPayView.hideProgress();

                    }
                });
    }

    /**
    * 上传支付宝授权码
    * */
    public void setAlipayAutoCode(String autoCode){
        mAliPayView.showProgress();
        Map<String, String> params = new HashMap<>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        params.put("code", autoCode);
        HttpUtils.post().url(coreManager.getConfig().ALIPAY_AUTH_CODE)
                .params(params)
                .build()
                .execute(new BaseCallback<String>(String.class) {

                    @Override
                    public void onResponse(ObjectResult<String> result) {
                        mAliPayView.AlipayAutoCodeResult(result);
                        mAliPayView.hideProgress();
                    }
                    @Override
                    public void onError(Call call, Exception e) {

                        Log.e("AlipayAutoMsg", "Exception", e);
                        mAliPayView.hideProgress();
                    }
                });
    }

    /**
    * 解绑支付宝
    * */
    public void UnBindAlipay(){
        mAliPayView.showProgress();
        Map<String, String> params = new HashMap<>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        HttpUtils.post().url(coreManager.getConfig().ALIPAY_UNBIND)
                .params(params)
                .build()
                .execute(new BaseCallback<String>(String.class) {

                    @Override
                    public void onResponse(ObjectResult<String> result) {
                        if (result.getResultCode()==1){
                            mAliPayView.AlipayUnbindResult(result);
                            mAliPayView.hideProgress();
                        }
                    }
                    @Override
                    public void onError(Call call, Exception e) {
                        Log.e("UnBindAlipay", "Exception", e);
                        mAliPayView.hideProgress();

                    }
                });
    }
}
